package com.kust.eduService.controller;


import com.kust.commomUtils.Result;
import com.kust.eduService.client.VodClient;
import com.kust.eduService.entity.Video;
import com.kust.eduService.service.EduVideoService;
import com.kust.serviceBase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-23
 */
@RestController
@RequestMapping("/eduService/edu-video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody Video video){
        videoService.save(video);
        return  Result.ok();
    }

    //删除小节
    // TODO 这个后面需要完善 删除小节的时候 把视频也删了
    @DeleteMapping("{id}")
    public Result deleteVideo(@PathVariable String id){

        //根据小节id获得视频id
        Video video = videoService.getById(id);
        //根据视频id，远程调用实现视频删除
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            Result result = vodClient.removeALYVide(videoSourceId);//Todo 已完善
            if(result.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器执行...");
            }
        }
        videoService.removeById(id);
        return Result.ok();
    }

    //修改小节 TODO

}

